package cn.wubo.file.preview.demo;

import cn.wubo.file.storage.core.FileInfo;
import cn.wubo.file.storage.record.IFileStroageRecord;
import cn.wubo.sql.util.*;
import com.alibaba.druid.DbType;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.UUID;

@Component
public class H2FileStroageRecordImpl implements IFileStroageRecord {

    private static final String HISTORY = "file_storage_history";

    private static ConnectionPool connectionPool = new ConnectionPool(new ConnectionParam());

    @Override
    public FileInfo save(FileInfo fileInfo) {
        if (!StringUtils.hasLength(fileInfo.getId())) {
            fileInfo.setId(UUID.randomUUID().toString());
            connectionPool.run((connection, sql) -> {
                return ExecuteSqlUtils.executeUpdate(connection, sql);
            }, ModelSqlUtils.insertSql(HISTORY, fileInfo));
        } else {
            connectionPool.run((connection, sql) -> {
                return ExecuteSqlUtils.executeUpdate(connection, sql);
            }, ModelSqlUtils.updateByIdSql(HISTORY, fileInfo));
        }
        return fileInfo;
    }

    @Override
    public List<FileInfo> list(FileInfo fileInfo) {
        SQL<FileInfo> sql = ModelSqlUtils.selectSql(HISTORY, new FileInfo());
        if (StringUtils.hasLength(fileInfo.getId())) sql.addWhereEQ("id", fileInfo.getId());
        if (StringUtils.hasLength(fileInfo.getPlatform())) sql.addWhereEQ("platform", fileInfo.getPlatform());
        if (StringUtils.hasLength(fileInfo.getAlias())) sql.addWhereLIKE("alias", fileInfo.getAlias());
        if (StringUtils.hasLength(fileInfo.getOriginalFilename()))
            sql.addWhereLIKE("originalFilename", fileInfo.getOriginalFilename());
        sql.parse();
        return connectionPool.run((connection, fileInfoSQL) -> {
            return ExecuteSqlUtils.executeQuery(connection, fileInfoSQL);
        }, sql);
    }

    @Override
    public FileInfo findById(String s) {
        FileInfo query = new FileInfo();
        query.setId(s);
        List<FileInfo> list = list(query);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public Boolean delete(FileInfo fileInfo) {
        SQL<?> sql = SQL.delete().table(HISTORY);
        if (StringUtils.hasLength(fileInfo.getId())) sql.addWhereEQ("id", fileInfo.getId());
        if (StringUtils.hasLength(fileInfo.getPlatform())) sql.addWhereEQ("platform", fileInfo.getPlatform());
        if (StringUtils.hasLength(fileInfo.getAlias())) sql.addWhereLIKE("alias", fileInfo.getAlias());
        if (StringUtils.hasLength(fileInfo.getOriginalFilename()))
            sql.addWhereLIKE("originalFilename", fileInfo.getOriginalFilename());
        sql.parse();
        connectionPool.run((connection, fileInfoSQL) -> {
            return ExecuteSqlUtils.executeUpdate(connection, fileInfoSQL);
        }, sql);
        return Boolean.TRUE;
    }

    @Override
    public void init() {
        if (Boolean.FALSE.equals(connectionPool.run((connection, sql) -> {
            return ExecuteSqlUtils.isTableExists(connection, sql);
        }, SQL.tableExists(HISTORY, DbType.h2)))) {
            connectionPool.run((connection, sqlStr) -> {
                return ExecuteSqlUtils.executeUpdate(connection, sqlStr);
            }, ModelSqlUtils.createSql(HISTORY, FileInfo.class));
        }
    }
}
